import 'package:mobx/mobx.dart';
import 'package:thal/backstage/blueprint/lake_recommend_print.dart';

part 'lake_recommend_room.g.dart';

/// 湖泊推荐状态管理
class LakeRecommendRoom = _LakeRecommendRoom with _$LakeRecommendRoom;

abstract class _LakeRecommendRoom with Store {
  /// 湖泊推荐列表
  @observable
  ObservableList<LakeRecommendPrint> lakeRecommends =
      ObservableList<LakeRecommendPrint>();

  /// 初始化默认湖泊推荐数据
  @action
  void initializeDefaultLakeRecommends() {
    lakeRecommends.clear();
    lakeRecommends.addAll([
      LakeRecommendPrint(
        id: '1',
        title: 'Crater Lake',
        location: 'America',
        description:
            'Crater Lake, located in the Cascade Mountains of Oregon, USA, is the deepest lake in the United States and the seventh deepest in the world. It is also a natural wonder created by volcanic activity.\n\nIt was formed about 7,700 years ago. After a large-scale eruption of Mount Mazama, the summit collapsed to form a crater, which was accumulated over a long period of time by rain and snow. The lake water is pure with no water inlet or outlet. It is replenished by ice and snow, presenting a deep and deep blue color with extremely high transparency. The Wizard Island in the center of the lake is a small volcanic cone, adding a unique charm to the lake.\n\nNowadays, this place has been included in Crater Lake National Park, surrounded by forests and mountains. It is a popular destination for enjoying the scenery of the volcanic lake, hiking and camping.',
        imageUrl: 'assets/images/lake1.png',
        createdAt: DateTime.now().subtract(const Duration(days: 1)),
      ),
      LakeRecommendPrint(
        id: '2',
        title: 'Lake Baikal',
        location: 'Russia',
        description:
            'Lake Baikal, located in the southern part of eastern Siberia, Russia, is known as the "blue Eye of Siberia". It is the deepest, largest in water storage capacity and oldest freshwater lake in the world.\n\nIt was formed about 25 million years ago during a crustal fracture movement, with a maximum depth of 1,637 meters and a water storage capacity accounting for 20% of the world\'s total surface fresh water. The lake water is extremely clear, with a transparency of over 40 meters.\n\nThe scenery varies greatly throughout the four seasons, and the "blue ice" wonder in winter is particularly famous. Eighty percent of the animals and plants in the lake are endemic species, including the world\'s only freshwater seal, which has unique ecological value. In 1996, Lake Baikal was inscribed on the World Natural Heritage List and has also become a popular eco-tourism destination.',
        imageUrl: 'assets/images/lake2.png',
        createdAt: DateTime.now().subtract(const Duration(days: 2)),
      ),
      LakeRecommendPrint(
        id: '3',
        title: 'Jade Lake',
        location: 'Canada',
        description:
            'It is located in Yoho National Park, British Columbia, Canada, and is the largest lake in the park. In 1882, the renowned mountain guide Tom Wilson discovered it while looking for a lost horse. \n\nDue to the bright emerald green color of the lake water, he named it Emerald Lake. The lake covers an area of approximately 1 square kilometer and is 28 meters deep. \n\nThere is a 5.2-kilometer hiking trail surrounding the lake, and the first half of the trail is friendly to wheelchairs and baby strollers. Tourists can also rent canoes or go boating here. Summer fishing is also very popular. In addition, there is the Jade Lake Hotel by the lake, which offers accommodation services.',
        imageUrl: 'assets/images/lake3.png',
        createdAt: DateTime.now().subtract(const Duration(days: 3)),
      ),
    ]);
  }

  /// 获取所有湖泊推荐
  List<LakeRecommendPrint> fetchAllLakeRecommends() {
    // 如果数据为空，自动初始化默认数据
    if (lakeRecommends.isEmpty) {
      initializeDefaultLakeRecommends();
    }
    return List.from(lakeRecommends);
  }

  /// 根据ID获取湖泊推荐
  LakeRecommendPrint? fetchLakeRecommendById(String id) {
    try {
      return lakeRecommends.firstWhere((lake) => lake.id == id);
    } catch (e) {
      return null;
    }
  }

  /// 添加新的湖泊推荐
  @action
  bool addLakeRecommend({
    required String title,
    required String location,
    required String description,
    required String imageUrl,
  }) {
    try {
      final newLake = LakeRecommendPrint(
        id: (lakeRecommends.length + 1).toString(),
        title: title,
        location: location,
        description: description,
        imageUrl: imageUrl,
        createdAt: DateTime.now(),
      );
      lakeRecommends.add(newLake);
      return true;
    } catch (e) {
      return false;
    }
  }

  /// 更新湖泊推荐信息
  @action
  bool updateLakeRecommend(LakeRecommendPrint updatedLake) {
    try {
      final index = lakeRecommends.indexWhere(
        (lake) => lake.id == updatedLake.id,
      );
      if (index != -1) {
        lakeRecommends[index] = updatedLake;
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  /// 删除湖泊推荐
  @action
  bool removeLakeRecommend(String id) {
    try {
      lakeRecommends.removeWhere((lake) => lake.id == id);
      return true;
    } catch (e) {
      return false;
    }
  }

  /// 获取湖泊推荐总数
  int get lakeRecommendCount => lakeRecommends.length;

  /// 根据地点搜索湖泊推荐
  List<LakeRecommendPrint> searchLakeRecommendsByLocation(String location) {
    return lakeRecommends
        .where(
          (lake) =>
              lake.location.toLowerCase().contains(location.toLowerCase()),
        )
        .toList();
  }

  /// 根据标题搜索湖泊推荐
  List<LakeRecommendPrint> searchLakeRecommendsByTitle(String title) {
    return lakeRecommends
        .where((lake) => lake.title.toLowerCase().contains(title.toLowerCase()))
        .toList();
  }
}
